home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / patch / patch.man.nroffed < prev    next >
Text File  |  1995-03-18  |  14KB  |  329 lines

  1.  
  2. PATCH(1)            UNIX Programmer's Manual             PATCH(1)
  3.  
  4. NAME
  5.      patch - a program for applying a diff file to an original
  6.  
  7. SYNOPSIS
  8.      patch [options] orig patchfile [+ [options] orig]
  9.  
  10.      but usually just
  11.  
  12.      patch <patchfile
  13.  
  14. DESCRIPTION
  15.      Patch will take a patch file containing any of the three
  16.      forms of difference listing produced by the diff program and
  17.      apply those differences to an original file, producing a
  18.      patched version.  By default, the patched version is put in
  19.      place of the original, with the original file backed up to
  20.      the same name with the extension ".orig", or as specified by
  21.      the -b switch.  You may also specify where you want the out-
  22.      put to go with a -o switch.  If patchfile is omitted, or is
  23.      a hyphen, the patch will be read from standard input.
  24.  
  25.      Upon startup, patch will attempt to determine the type of
  26.      the diff listing, unless over-ruled by a -c, -e, or -n
  27.      switch.  Context diffs and normal diffs are applied by the
  28.      patch program itself, while ed diffs are simply fed to the
  29.      ed editor via a pipe.
  30.  
  31.      Patch will try to skip any leading garbage, apply the diff,
  32.      and then skip any trailing garbage.  Thus you could feed an
  33.      article or message containing a diff listing to patch, and
  34.      it should work.  If the entire diff is indented by a con-
  35.      sistent amount, this will be taken into account.
  36.  
  37.      With context diffs, and to a lesser extent with normal
  38.      diffs, patch can detect when the line numbers mentioned in
  39.      the patch are incorrect, and will attempt to find the
  40.      correct place to apply each hunk of the patch.  As a first
  41.      guess, it takes the line number mentioned for the hunk, plus
  42.      or minus any offset used in applying the previous hunk.  If
  43.      that is not the correct place, patch will scan both forwards
  44.      and backwards for a set of lines matching the context given
  45.      in the hunk.  First patch looks for a place where all lines
  46.      of the context match.  If no such place is found, and it's a
  47.      context diff, and the maximum fuzz factor is set to 1 or
  48.      more, then another scan takes place ignoring the first and
  49.      last line of context.  If that fails, and the maximum fuzz
  50.      factor is set to 2 or more, the first two and last two lines
  51.      of context are ignored, and another scan is made.  (The
  52.      default maximum fuzz factor is 2.) If patch cannot find a
  53.      place to install that hunk of the patch, it will put the
  54.      hunk out to a reject file, which normally is the name of the
  55.      output file plus ".rej".  (Note that the rejected hunk will
  56.  
  57. Printed 3/6/88                LOCAL                             1
  58.  
  59. PATCH(1)            UNIX Programmer's Manual             PATCH(1)
  60.  
  61.      come out in context diff form whether the input patch was a
  62.      context diff or a normal diff.  If the input was a normal
  63.      diff, many of the contexts will simply be null.) The line
  64.      numbers on the hunks in the reject file may be different
  65.      than in the patch file: they reflect the approximate loca-
  66.      tion patch thinks the failed hunks belong in the new file
  67.      rather than the old one.
  68.  
  69.      As each hunk is completed, you will be told whether the hunk
  70.      succeeded or failed, and which line (in the new file) patch
  71.      thought the hunk should go on.  If this is different from
  72.      the line number specified in the diff you will be told the
  73.      offset.  A single large offset MAY be an indication that a
  74.      hunk was installed in the wrong place.  You will also be
  75.      told if a fuzz factor was used to make the match, in which
  76.      case you should also be slightly suspicious.
  77.  
  78.      If no original file is specified on the command line, patch
  79.      will try to figure out from the leading garbage what the
  80.      name of the file to edit is.  In the header of a context
  81.      diff, the filename is found from lines beginning with "***"
  82.      or "---", with the shortest name of an existing file win-
  83.      ning.  Only context diffs have lines like that, but if there
  84.      is an "Index:" line in the leading garbage, patch will try
  85.      to use the filename from that line.  The context diff header
  86.      takes precedence over an Index line.  If no filename can be
  87.      intuited from the leading garbage, you will be asked for the
  88.      name of the file to patch.
  89.  
  90.      (If the original file cannot be found, but a suitable SCCS
  91.      or RCS file is handy, patch will attempt to get or check out
  92.      the file.)
  93.  
  94.      Additionally, if the leading garbage contains a "Prereq: "
  95.      line, patch will take the first word from the prerequisites
  96.      line (normally a version number) and check the input file to
  97.      see if that word can be found.  If not, patch will ask for
  98.      confirmation before proceeding.
  99.  
  100.      The upshot of all this is that you should be able to say,
  101.      while in a news interface, the following:
  102.  
  103.           | patch -d /usr/src/local/blurfl
  104.  
  105.      and patch a file in the blurfl directory directly from the
  106.      article containing the patch.
  107.  
  108.      If the patch file contains more than one patch, patch will
  109.      try to apply each of them as if they came from separate
  110.      patch files.  This means, among other things, that it is
  111.      assumed that the name of the file to patch must be deter-
  112.      mined for each diff listing, and that the garbage before
  113.  
  114. Printed 3/6/88                LOCAL                             2
  115.  
  116. PATCH(1)            UNIX Programmer's Manual             PATCH(1)
  117.  
  118.      each diff listing will be examined for interesting things
  119.      such as filenames and revision level, as mentioned previ-
  120.      ously.  You can give switches (and another original file
  121.      name) for the second and subsequent patches by separating
  122.      the corresponding argument lists by a '+'.  (The argument
  123.      list for a second or subsequent patch may not specify a new
  124.      patch file, however.)
  125.  
  126.      Patch recognizes the following switches:
  127.  
  128.      -b   causes the next argument to be interpreted as the
  129.           backup extension, to be used in place of ".orig".
  130.  
  131.      -c   forces patch to interpret the patch file as a context
  132.           diff.
  133.  
  134.      -d   causes patch to interpret the next argument as a direc-
  135.           tory, and cd to it before doing anything else.
  136.  
  137.      -D   causes patch to use the "#ifdef...#endif" construct to
  138.           mark changes.  The argument following will be used as
  139.           the differentiating symbol.  Note that, unlike the C
  140.           compiler, there must be a space between the -D and the
  141.           argument.
  142.  
  143.      -e   forces patch to interpret the patch file as an ed
  144.           script.
  145.  
  146.      -f   forces patch to assume that the user knows exactly what
  147.           he or she is doing, and to not ask any questions.  It
  148.           does not suppress commentary, however.  Use -s for
  149.           that.
  150.  
  151.      -F<number>
  152.           sets the maximum fuzz factor.  This switch only applied
  153.           to context diffs, and causes patch to ignore up to that
  154.           many lines in looking for places to install a hunk.
  155.           Note that a larger fuzz factor increases the odds of a
  156.           faulty patch.  The default fuzz factor is 2, and it may
  157.           not be set to more than the number of lines of context
  158.           in the context diff, ordinarily 3.
  159.  
  160.      -l   causes the pattern matching to be done loosely, in case
  161.           the tabs and spaces have been munged in your input
  162.           file.  Any sequence of whitespace in the pattern line
  163.           will match any sequence in the input file.  Normal
  164.           characters must still match exactly.  Each line of the
  165.           context must still match a line in the input file.
  166.  
  167.      -n   forces patch to interpret the patch file as a normal
  168.           diff.
  169.  
  170. Printed 3/6/88                LOCAL                             3
  171.  
  172. PATCH(1)            UNIX Programmer's Manual             PATCH(1)
  173.  
  174.      -N   causes patch to ignore patches that it thinks are
  175.           reversed or already applied.  See also -R .
  176.  
  177.      -o   causes the next argument to be interpreted as the out-
  178.           put file name.
  179.  
  180.      -p<number>
  181.           sets the pathname strip count, which controls how path-
  182.           names found in the patch file are treated, in case the
  183.           you keep your files in a different directory than the
  184.           person who sent out the patch.  The strip count speci-
  185.           fies how many backslashes are to be stripped from the
  186.           front of the pathname.  (Any intervening directory
  187.           names also go away.) For example, supposing the
  188.           filename in the patch file was
  189.  
  190.                /u/howard/src/blurfl/blurfl.c
  191.  
  192.           setting -p or -p0 gives the entire pathname unmodified,
  193.           -p1 gives
  194.  
  195.                u/howard/src/blurfl/blurfl.c
  196.  
  197.           without the leading slash, -p4 gives
  198.  
  199.                blurfl/blurfl.c
  200.  
  201.           and not specifying -p at all just gives you "blurfl.c".
  202.           Whatever you end up with is looked for either in the
  203.           current directory, or the directory specified by the -d
  204.           switch.
  205.  
  206.      -r   causes the next argument to be interpreted as the
  207.           reject file name.
  208.  
  209.      -R   tells patch that this patch was created with the old
  210.           and new files swapped.  (Yes, I'm afraid that does hap-
  211.           pen occasionally, human nature being what it is.) Patch
  212.           will attempt to swap each hunk around before applying
  213.           it.  Rejects will come out in the swapped format.  The
  214.           -R switch will not work with ed diff scripts because
  215.           there is too little information to reconstruct the
  216.           reverse operation.
  217.  
  218.           If the first hunk of a patch fails, patch will reverse
  219.           the hunk to see if it can be applied that way.  If it
  220.           can, you will be asked if you want to have the -R
  221.           switch set.  If it can't, the patch will continue to be
  222.           applied normally.  (Note: this method cannot detect a
  223.           reversed patch if it is a normal diff and if the first
  224.           command is an append (i.e. it should have been a
  225.           delete) since appends always succeed, due to the fact
  226.  
  227. Printed 3/6/88                LOCAL                             4
  228.  
  229. PATCH(1)            UNIX Programmer's Manual             PATCH(1)
  230.  
  231.           that a null context will match anywhere.  Luckily, most
  232.           patches add or change lines rather than delete them, so
  233.           most reversed normal diffs will begin with a delete,
  234.           which will fail, triggering the heuristic.)
  235.  
  236.      -s   makes patch do its work silently, unless an error
  237.           occurs.
  238.  
  239.      -S   causes patch to ignore this patch from the patch file,
  240.           but continue on looking for the next patch in the file.
  241.           Thus
  242.  
  243.                patch -S + -S + <patchfile
  244.  
  245.           will ignore the first and second of three patches.
  246.  
  247.      -v   causes patch to print out it's revision header and
  248.           patch level.
  249.  
  250.      -x<number>
  251.           sets internal debugging flags, and is of interest only
  252.           to patch patchers.
  253.  
  254. ENVIRONMENT
  255.      No environment variables are used by patch.
  256.  
  257. FILES
  258.      /tmp/patch*
  259.  
  260. SEE ALSO
  261.      diff(1)
  262.  
  263. NOTES FOR PATCH SENDERS
  264.      There are several things you should bear in mind if you are
  265.      going to be sending out patches.  First, you can save people
  266.      a lot of grief by keeping a patchlevel.h file which is
  267.      patched to increment the patch level as the first diff in
  268.      the patch file you send out.  If you put a Prereq: line in
  269.      with the patch, it won't let them apply patches out of order
  270.      without some warning.  Second, make sure you've specified
  271.      the filenames right, either in a context diff header, or
  272.      with an Index: line.  If you are patching something in a
  273.      subdirectory, be sure to tell the patch user to specify a -p
  274.      switch as needed.  Third, you can create a file by sending
  275.      out a diff that compares a null file to the file you want to
  276.      create.  This will only work if the file you want to create
  277.      doesn't exist already in the target directory.  Fourth, take
  278.      care not to send out reversed patches, since it makes people
  279.      wonder whether they already applied the patch.  Fifth, while
  280.      you may be able to get away with putting 582 diff listings
  281.      into one file, it is probably wiser to group related patches
  282.      into separate files in case something goes haywire.
  283.  
  284. Printed 3/6/88                LOCAL                             5
  285.  
  286. PATCH(1)            UNIX Programmer's Manual             PATCH(1)
  287.  
  288. DIAGNOSTICS
  289.      Too many to list here, but generally indicative that patch
  290.      couldn't parse your patch file.
  291.  
  292.      The message "Hmm..." indicates that there is unprocessed
  293.      text in the patch file and that patch is attempting to
  294.      intuit whether there is a patch in that text and, if so,
  295.      what kind of patch it is.
  296.  
  297. CAVEATS
  298.      Patch cannot tell if the line numbers are off in an ed
  299.      script, and can only detect bad line numbers in a normal
  300.      diff when it finds a "change" or a "delete" command.  A con-
  301.      text diff using fuzz factor 3 may have the same problem.
  302.      Until a suitable interactive interface is added, you should
  303.      probably do a context diff in these cases to see if the
  304.      changes made sense.  Of course, compiling without errors is
  305.      a pretty good indication that the patch worked, but not
  306.      always.
  307.  
  308.      Patch usually produces the correct results, even when it has
  309.      to do a lot of guessing.  However, the results are
  310.      guaranteed to be correct only when the patch is applied to
  311.      exactly the same version of the file that the patch was gen-
  312.      erated from.
  313.  
  314. BUGS
  315.      Could be smarter about partial matches, excessively deviant
  316.      offsets and swapped code, but that would take an extra pass.
  317.  
  318.      If code has been duplicated (for instance with #ifdef OLD-
  319.      CODE ... #else ...  #endif), patch is incapable of patching
  320.      both versions, and, if it works at all, will likely patch
  321.      the wrong one, and tell you that it succeeded to boot.
  322.  
  323.      If you apply a patch you've already applied, patch will
  324.      think it is a reversed patch, and offer to un-apply the
  325.      patch.  This could be construed as a feature.
  326.  
  327. Printed 3/6/88                LOCAL                             6
  328.  
  329.